﻿namespace Yom.Common
{
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Web.Caching;

    /// <summary>
    /// 缓存操作类
    /// 
    /// 创建: 邱柯皓 2009/02/05 22:07
    /// </summary>
    public class Caches
    {
        #region 私有成员定义
        private Cache ObjCache = HttpRuntime.Cache;
        private short TimeOut = 720;
        #endregion

        #region 成员属性暴露
        public object this[string index]
        {
            get { return GetCache(index); }
            set { SetCache(index, value); }
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 获取缓存
        /// 
        /// 创建: 邱柯皓 2009/02/05 22:12
        /// </summary>
        /// <param name="CacheKey">缓存关键字</param>
        /// <returns>缓存数据</returns>
        public object GetCache(string CacheKey)
        {
            return ObjCache[CacheKey];
        }

        /// <summary>
        /// 设置缓存
        /// 
        /// 创建: 邱柯皓 2009/02/05 22:13
        /// </summary>
        /// <param name="CacheKey">缓存关键字</param>
        /// <param name="CacheData">缓存数据</param>
        public void SetCache(string CacheKey, object CacheData)
        {
            ObjCache.Insert(CacheKey, RuntimeHelpers.GetObjectValue(CacheData));
        }

        /// <summary>
        /// 设置缓存
        /// 
        /// 创建: 邱柯皓 2009/02/05 22:15
        /// </summary>
        /// <param name="CacheKey">缓存关键字</param>
        /// <param name="CacheData">缓存数据</param>
        /// <param name="TimeOuts">超时时间</param>
        public void SetCache(string CacheKey, object CacheData, int TimeOuts)
        {
            ObjCache.Insert(CacheKey, RuntimeHelpers.GetObjectValue(CacheData), null, DateTime.Now.AddMinutes((double)TimeOuts), TimeSpan.Zero);
        }

        /// <summary>
        /// 设置缓存
        /// 
        /// 创建: 邱柯皓 2009/02/05 22:18
        /// </summary>
        /// <param name="CacheKey">缓存关键字</param>
        /// <param name="CacheData">缓存数据</param>
        /// <param name="Files">缓存依赖文件</param>
        public void SetCache(string CacheKey, object CacheData, string Files)
        {
            CacheDependency CacheDep = new CacheDependency(Files, DateTime.Now);
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(CacheData), TimeOut, CacheDep, CacheItemPriority.High);
        }

        /// <summary>
        /// 设置缓存
        /// 
        /// 创建: 邱柯皓 2009/02/05 22:19
        /// </summary>
        /// <param name="CacheKey">缓存关键字</param>
        /// <param name="CacheData">缓存数据</param>
        /// <param name="Priority">缓存级别</param>
        public void SetCache(string CacheKey, object CacheData, CacheItemPriority Priority)
        {
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(CacheData), TimeOut, null, Priority);
        }

        /// <summary>
        /// 设置缓存
        /// 
        /// 创建: 邱柯皓 2009/02/05 22:21
        /// </summary>
        /// <param name="CacheKey">缓存关键字</param>
        /// <param name="CacheData">缓存数据</param>
        /// <param name="TimeOuts">超时时间</param>
        /// <param name="CacheDep">依赖文件</param>
        /// <param name="Priority">缓存级别</param>
        public void SetCache(string CacheKey, object CacheData, int TimeOuts, CacheDependency CacheDep, CacheItemPriority Priority)
        {
            ObjCache.Insert(CacheKey, RuntimeHelpers.GetObjectValue(CacheData), CacheDep, DateTime.MaxValue, TimeSpan.FromHours((double)TimeOuts), Priority, null);
        }

        /// <summary>
        /// 清空缓存
        /// 
        /// 创建: 邱柯皓 2009/03/07 13:33
        /// </summary>
        public void ClearCache()
        {
            IDictionaryEnumerator CacheEnum = ObjCache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                ObjCache.Remove(CacheEnum.Key.ToString());
            }
        }
        #endregion
    }
}